package dao;
import java.io.FileWriter;
import java.util.*;

import org.dom4j.*;
import org.dom4j.io.*;

import po.Product;
import util.PathTool;
public class ProductDao {
	private static String tableName;
	private static String xmlPathr;
	private static Document document;
	
	
	static {
		tableName = "product";
		xmlPathr = PathTool.getWebPath() + "xmlData/" + tableName + ".xml";
		try {
			SAXReader sr = new SAXReader();
	        document = sr.read(xmlPathr);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static Product[] queryAllRecord() { // 查询所有
		Element root = document.getRootElement();
		List<Element> list = root.elements();
		Element temp;
		int amount = list.size();
		Product[] s = new Product[amount]; // 数组
		Product tempProduct;
		for (int i=0; i<amount; i++) {
			temp = list.get(i);
			tempProduct = new Product();
			tempProduct.setid(temp.attributeValue("id"));
			
			for (Iterator<Element> it = temp.elementIterator(); it.hasNext();) {
                Element node =  it.next();
                String type = node.getName();
                if ("info".equals(type)) {
                	tempProduct.setInfo(node.getText());
                } else if ("name".equals(type)) {
                	tempProduct.setName(node.getText());
                } else if ("price".equals(type)) {
                	tempProduct.setPrice(Integer.valueOf(node.getText()));
                }
            }
			s[i] = tempProduct;
		}
		return s;
	}
	
	public static boolean addOneRecord(Product p) { // 添加一条记录
		Element root = document.getRootElement();
		Element newE = root.addElement(tableName);
		// 添加主键
		newE.addAttribute("id", p.getId()); // 添加主键属性
		
		newE.addElement("name").setText(p.getName()); // 添加 属性
		newE.addElement("info").setText(p.getInfo());
		newE.addElement("price").setText(String.valueOf(p.getPrice()));
		writeToNewXMLDocument(); // 写入
		return true;
	}
	
	public static int getRecordAmount() {
		int amount=0; // 数量
		Element root = document.getRootElement();
		List<Element> list = root.elements();
		amount = list.size();
		return amount;
	}
	
	public static boolean queryOneRecord(Product p) { // 根据 主码 查询一条数据
		Element root = document.getRootElement();
		List<Element> userList =  root.elements(); // 所有记录组成的集合
		// 开始查找对应的 id
		Element temp;
		for (int i=0; i<userList.size(); i++) {
			temp = userList.get(i); // 一条记录
			// 判断 主码
			if (checkRecordPrimaryKey(p, temp)) {
				List<Element> userElementList = temp.elements();
				// 开始补齐数据
				for (int j=0; j<userElementList.size(); j++) {
					Element te = userElementList.get(j);
					String type = te.getName();
					if ("info".equals(type)) {
	                	p.setInfo(te.getText());
	                } else if ("name".equals(type)) {
	                	p.setName(te.getText());
	                } else if ("price".equals(type)) {
	                	p.setPrice(Integer.valueOf(te.getText()));
	                }
				}
				return true;
			}
		}
		return false; // 没找到
	}
	
	private static boolean checkRecordPrimaryKey (Product p, Element temp) {
		if (temp.attributeValue("id").equals(p.getId()))
			return true;
		return false;
	}
	
    private static void writeToNewXMLDocument() {
    	try {
            XMLWriter writer = new XMLWriter(new FileWriter(
            		xmlPathr));
            writer.write(document);
            writer.close();
    	} catch(Exception e) {
    		e.printStackTrace();
    	}
    }
}
